classSolution(object): defreverseString(self, s): """ :type s: List[str] :rtype: None Do not return anything, modify s in-place instead. """ # 双指针法, 第一个和最后交换 left = 0 right = len(s) - 1 # 不用判断个数是奇数个和偶数个。直接采用 while 循环。每次循环 left +1, right -1 while left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1
1 2 3 4 5 6 7 8 9 10
funcreverseString(s []byte) { left := 0 right := len(s) -1
for left < right{ s[left], s[right] = s[right], s[left] left +=1 right -=1 } }